package com.retailcloud.searchservice.controller;

import com.retailcloud.commonutils.dto.ProductSearchVo;
import com.retailcloud.searchservice.service.ProductService;
import com.retailcloud.commonutils.dto.ProductionInfoVo;
import com.retailcloud.commonutils.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author wudi
 * @date 2024/11/8
 * @comment
 */
@Slf4j
@RestController
@RequestMapping("/searchService")
public class ElasticSearchController {

    @Autowired
    ProductService productService;

    //上架商品
    @PostMapping("/save/product")
    public R productSave(@RequestBody List<ProductionInfoVo> productionInfoVos) {
        boolean b = false;
        try {
            b = productService.productStatusUp(productionInfoVos);
        } catch (Exception e) {
            log.error("ElasticSaveController商品上架错误 {}", e);
            return R.error(6001, "同步商品失败");
        }
        if (!b) {
            return R.ok();
        } else {
            return R.error(6001, "同步商品失败");
        }
    }

    /**
     * 查询
     * @param productSearchVo
     * @return
     */
    @PostMapping("/search/product")
    public Map<String, Object> productSearch(@RequestBody ProductSearchVo productSearchVo) {


        try {
            return productService.getProductInfoList(productSearchVo);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

}
